package com.imooc.web.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.imooc.dto.User;
import com.imooc.dto.UserQueryCondition;
import com.imooc.exception.UserNotExistException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zghgchao 2018/4/13 22:40
 */
@RestController
@RequestMapping("/user")
public class UserController {

    /*@GetMapping("/me")
    public Object getCurrentUser(){
        return SecurityContextHolder.getContext().getAuthentication();
    }*/

    /*@GetMapping("/me")
    public Object getCurrentUser(Authentication authentication){
        return authentication;
    }*/

    @GetMapping("/me")
    public Object getCurrentUser(@AuthenticationPrincipal UserDetails userDetails){
        return userDetails;
    }

//    @RequestMapping(value = "/user", method = RequestMethod.GET)
    @GetMapping()
//    @JsonView(User.UserSimpleView.class)
    @ApiOperation(value = "查询用户信息")
    public List<User> query(UserQueryCondition condition,
                            @PageableDefault(page = 2,size = 10,sort = "username,asc") Pageable pageable) {//@RequestParam() String username

        // 通过反射打印出 查询条件
        System.out.println(ReflectionToStringBuilder.toString(condition, ToStringStyle.MULTI_LINE_STYLE));

        System.out.println(pageable.getPageSize());
        System.out.println(pageable.getPageNumber());
        System.out.println(pageable.getSort());

        List<User> userList = new ArrayList<>();
        userList.add(new User());
        userList.add(new User());
        userList.add(new User());

        return userList;
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation(value = "根据用户id获取用户信息")
//    @JsonView(User.UserDetailView.class)
    public User getInfo(@PathVariable String id){
//        throw new RuntimeException("user not exist");
//        System.out.println(id);
//        throw new UserNotExistException(id);
//        throw new RuntimeException("User not exist");
        System.out.println("进入getInfo服务");
        User user = new User();
        user.setId("1");
        user.setName("tom");
        user.setPassword("aaaaa");
        return user;
    }

    @PostMapping
    @ApiOperation(value = "生成用户")
    public User create(@Valid @RequestBody User user){//, BindingResult bindingResult
       /* if (bindingResult.hasErrors()){
//            System.out.println("参数传递有误");
            bindingResult.getAllErrors().stream().forEach(error -> System.out.println(error.toString()));
        }*/

        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @PutMapping("/{id:\\d+}")
    @ApiOperation(value = "根据用户id进行更新")
    public User update(@Valid @RequestBody User user, BindingResult bindingResult) throws Exception{
        if (bindingResult.hasErrors()){
//            System.out.println("参数传递有误");
            bindingResult.getAllErrors().stream().forEach(error ->{
                FieldError fieldError = (FieldError)error;
                String message = fieldError.getField() +" : "+ fieldError.getDefaultMessage();
                System.out.println(message);
                    }
            );
        }

        System.out.println(user.getId());
        System.out.println(user.getName());
        System.out.println(user.getPassword());
        System.out.println(user.getBirthday());

        user.setId("1");
        return user;
    }

    @DeleteMapping("{id:\\d+}")
    @ApiOperation(value = "根据用户id进行删除")
    public void delete(@ApiParam(value = "用户id") @PathVariable String id){
        System.out.println(id);
    }
}
